package library.and.xps.com.step.view.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import com.jude.utils.JUtils;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.util.StepManager;
import library.and.xps.com.step.view.base.CheckPermissionsActivity;
import library.and.xps.com.step.R;
import library.and.xps.com.step.stepmodule.StepServiceDelegate;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.customview.ColorArcProgressBar;

public class MainActivity extends CheckPermissionsActivity implements StepServiceDelegate.StepListener, OnClickListener, CompoundButton.OnCheckedChangeListener {
    //StepServiceDelegate stepDeleget;
    ColorArcProgressBar colorProgressBar;
    ImageView iv_history, iv_settings;
    CheckBox checkBox;
    ImageView iv_cb_src;
    private NotificationManager nm;
    private NotificationCompat.Builder builder;
    PendingIntent contentIntent;
    String targetStep = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_cb_src = (ImageView) findViewById(R.id.iv_cb_src);
        iv_history = (ImageView) findViewById(R.id.iv_history);
        iv_settings = (ImageView) findViewById(R.id.iv_settings);
        iv_history.setOnClickListener(this);
        iv_settings.setOnClickListener(this);
        colorProgressBar = (ColorArcProgressBar) findViewById(R.id.color_progressBar);

        //stepDeleget = StepServiceDelegate.getNewInstance(getApplicationContext());
        targetStep = JUtils.getSharedPreference().getString(SPConstant.USER_STEP_PLAN, "9000");

        if (StepManager.getInstance().getStepToday() != StepManager.STEP_NORECORD_YET) {
           // stepDeleget.initStep(StepManager.getNewInstance().getStepToday());
            colorProgressBar.setCurrentValues(StepManager.getInstance().getStepToday());
            colorProgressBar.setTitle("今天已走");
            colorProgressBar.setUnit("步");
        } else {
            colorProgressBar.setCurrentValues(0);
            colorProgressBar.setUnit("今天还没有运动");
            colorProgressBar.setTitle("");
        }

        //stepDeleget.setStepListener(this);

        checkBox = (CheckBox) findViewById(R.id.cb_start_stop);
        checkBox.setOnCheckedChangeListener(this);

       // CommonUtils.replaceFragment(R.id.weather, new FragmentWeather(), getSupportFragmentManager());
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        cb_pause();

    }

    /**
     * 设置check暂停时候的img的padding
     */
    void cb_pause() {
        int padding = CommonUtils.dip2px(this, 5);
        int left = CommonUtils.dip2px(this, 10);
        iv_cb_src.setPadding(left, padding, padding, padding);
    }

    /**
     * 设置check播放时候的img的padding
     */
    void cb_play() {
        int padding = CommonUtils.dip2px(this, 5);
        iv_cb_src.setPadding(padding, padding, padding, padding);
    }

    @Override
    public void step(int step) {
        if (!checkBox.isChecked() && nm != null) {
            nm.cancelAll();
            return;
        }
        updateNotification(step + "");
        colorProgressBar.setCurrentValues(step);
    }

    void setColorProgressBarPlayMode() {
        colorProgressBar.setTitle("今天已走");
        colorProgressBar.setUnit("步");
    }

    private void updateNotification(String content) {
        builder = new NotificationCompat.Builder(this);
        builder.setPriority(Notification.PRIORITY_MIN);
        builder.setContentIntent(contentIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker("StepLab!");
        builder.setContentTitle("今日步数" + content + "步");
        builder.setOngoing(true);

        builder.setContentText("目标步数" + targetStep + "步");
        nm.notify("StabLab!", 0, builder.build());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_exit:
//                if (stepDeleget != null)
//                    stepDeleget.callunbindService();
//                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_history:
                startActivity(new Intent(this, WorkOutActivity.class));
               // startActivity(new Intent(this, HistoryActivity.class));
                break;
            case R.id.iv_settings:
                startActivity(new Intent(this, SettingActivity.class));
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        if (isChecked) {
//            stepDeleget.callbindService();
//            cb_play();
//            iv_cb_src.setImageResource(R.drawable.pause);
//            setColorProgressBarPlayMode();
//            targetStep = JUtils.getSharedPreference().getString(SPConstant.USER_STEP_PLAN, "9000");
//        } else {
//            nm.cancel(0);
//            cb_pause();
//            stepDeleget.callunbindService();
//            iv_cb_src.setImageResource(R.drawable.play);
//            targetStep = JUtils.getSharedPreference().getString(SPConstant.USER_STEP_PLAN, "9000");
//        }
    }
}
